<?php
require_once __DIR__."/Base_model.php";
class Host_quailty_material_model extends Base_model
{
    public function __construct()
    {
        parent::__construct();
        $this->table = "quality_video";
        $this->table1 = "type";
        $this->table2 = "type_sub";
        $this->table3 = "user_material";
        $this->table4 = "users";
        $this->table5 = "video";
    }

    /**
     * 原始和弃用主页查询
     */
    public function all($arr){
        $this->db->select('*');
        $this->db->from($this->table);
        //查询为未删除视频
        $this->db->where("del3",2);
        $this->db->where("online",2);
        //不是空的上下线状态
        if(!empty($arr['online'])){
            $this->db->where("online",$arr['online']);
        }
        //不是空的分类筛选
        if(!empty($arr['join_type_sub'])){
            $this->db->where("quality_join_type_sub",$arr['join_type_sub']);
        }
        //不是空的分类筛选
        if(!empty($arr['is_join'])){
            $this->db->where("is_join",$arr['is_join']);
        }
        //时间筛选
        if(!empty($arr['start_time']) && !empty($arr['end_time'])){
            $this->db->where("add_time >=",$arr['start_time']);
            $this->db->where("add_time <",$arr['end_time']);
        }
        //分页
        if(!empty($arr['page']) && !empty($arr['limit'])){
            $pages=($arr['page']-1)*$arr['limit'];
            $datas['total']=$this->db->count_all_results("",false);
            $this->db->limit($arr['limit'],$pages);
        }else{
            $datas['total']=$this->db->count_all_results("",false);
        }
        //排序
        $this->db->order_by('add_time',"desc");
        $res = $this->db->get();
        $datas['row']= $res->result_array();
//        echo $this->db->last_query();
        return $datas;
    }

    /**
     * 原始和弃用主页查询
     */
    public function drop($arr){
        $this->db->select('*');
        $this->db->from($this->table);
        //查询为未删除视频
        $this->db->where("del3",2);
        $this->db->where("online",1);
        //不是空的上下线状态
        if(!empty($arr['online'])){
            $this->db->where("online",$arr['online']);
        }
        //不是空的分类筛选
        if(!empty($arr['join_type_sub'])){
            $this->db->where("quality_join_type_sub",$arr['join_type_sub']);
        }
        //不是空的分类筛选
        if(!empty($arr['is_join'])){
            $this->db->where("is_join",$arr['is_join']);
        }
        //时间筛选
        if(!empty($arr['start_time']) && !empty($arr['end_time'])){
            $this->db->where("add_time >=",$arr['start_time']);
            $this->db->where("add_time <",$arr['end_time']);
        }
        //分页
        if(!empty($arr['page']) && !empty($arr['limit'])){
            $pages=($arr['page']-1)*$arr['limit'];
            $datas['total']=$this->db->count_all_results("",false);
            $this->db->limit($arr['limit'],$pages);
        }else{
            $datas['total']=$this->db->count_all_results("",false);
        }
        //排序
        $this->db->order_by('add_time',"desc");
        $res = $this->db->get();
        $datas['row']= $res->result_array();
//        echo $this->db->last_query();
        return $datas;
    }

    /**
     * 加入普通列表支持事物回
     */
    public function add_material($data){
        //拼接添加到用户投稿列表的数据
        $datas['uuid']=$data['uuid'];
        $datas['nick_name']=$data['nick_name'];
        $datas['nick_pic']=$data['nick_pic'];
        $datas['video_uuid']=$data['quality_uuid'];
        $datas['pic']=$data['quatily_pic'];
        $datas['share_pic']=$data['quality_share_pic'];
        $datas['join_type']=$data['quality_join_type'];
        $datas['join_type_sub']=$data['quality_join_type_sub'];
//        $datas['video_url']=$data['quality_video_url'];
        $datas['video_desc']=$data['quality_desc'];
        $datas['duration']=$data['duration'];
        $datas['examine']=3;
        $datas['type']=1;
        $datas['quality']=1;//加入普通列表
        $datas['is_dub']=3;
        $datas['mix_status']=1;
        $datas['is_user_zj']=2;
        $datas['add_time']=date("Y-m-d H:i:s",time());
        $datas['width2']=$data['width1'];
        $datas['height2']=$data['height1'];
        $datas['first_pic3']=$data['first_pic2'];
        $this->db->trans_begin();// 开启事务
        //添加
        $this->db->insert($this->table3,$datas);
        $user_material_id=$this->db->insert_id();
        //受影响的行数
        $arr=$this->db->affected_rows();

        $data['user_material_id']=$user_material_id;
        $data['is_join']=2;
        $data['online']=2;
        //添加
        $this->db->insert($this->table,$data);
        $id=$this->db->insert_id();


        //添加

        if($this->db->trans_status()===false || $arr==0){
            $this->db->trans_rollback();//回滚
            return false;
        }else{
            $this->db->trans_commit();//提交事物

            return $id;
        }
    }



    /**
     * 加入精选列表支持事物回滚
     */
    public function add_join_material($data){
        //拼接添加到用户投稿列表的数据
        $datas['uuid']=$data['uuid'];
        $datas['nick_name']=$data['nick_name'];
        $datas['nick_pic']=$data['nick_pic'];
        $datas['video_uuid']=$data['quality_uuid'];
        $datas['pic']=$data['quatily_pic'];
        $datas['share_pic']=$data['quality_share_pic'];
        $datas['join_type']=$data['quality_join_type'];
        $datas['join_type_sub']=$data['quality_join_type_sub'];
//        $datas['video_url']=$data['quality_video_url'];
        $datas['video_desc']=$data['quality_desc'];
        $datas['duration']=$data['duration'];
        $datas['examine']=3;
        $datas['type']=1;
        $datas['quality']=2;//加入精选列表
        $datas['is_dub']=3;
        $datas['mix_status']=1;
        $datas['is_user_zj']=2;
        $datas['add_time']=date("Y-m-d H:i:s",time());
        $datas['width2']=$data['width1'];
        $datas['height2']=$data['height1'];
        $datas['first_pic3']=$data['first_pic2'];
        $this->db->trans_begin();// 开启事务
        //添加
        $this->db->insert($this->table3,$datas);
        $user_material_id=$this->db->insert_id();
        //受影响的行数
        $arr=$this->db->affected_rows();

        $data['user_material_id']=$user_material_id;
        $data['is_join']=2;
        $data['online']=2;
        //添加
        $this->db->insert($this->table,$data);
        $id=$this->db->insert_id();


        //添加

        if($this->db->trans_status()===false || $arr==0){
            $this->db->trans_rollback();//回滚
            return false;
        }else{
            $this->db->trans_commit();//提交事物

            return $id;
        }

    }

    /**
     * 把精品加入到话题
     */
    public function edit_join_material($data){
        //1.先去查询user_material表是否有这个记录
        $this->db->select('*');
        $this->db->from($this->table3);
        $this->db->where("id",$data['user_material_id']);
        $res = $this->db->get();
        $da1= $res->row_array();
        //
        if(!empty($da1)){
            //不为空
            $this->db->trans_begin();// 开启事务
            $da2['examine']=3;
            $this->db->update($this->table3,$da2,['id'=>$da1['id']]);
            //受影响的行数
            $arr=$this->db->affected_rows();
            //添加
            $this->db->update($this->table,$data,['id'=>$data['id']]);

            if($this->db->trans_status()===false || $arr==0){
                $this->db->trans_rollback();//回滚
                return false;
            }else{
                $this->db->trans_commit();//提交事物

                return true;
            }
        }else{
            //为空
            //拼接添加到用户投稿列表的数据
            $datas['uuid']=1;
            $datas['video_uuid']=$data['quality_uuid'];
            $datas['pic']=$data['quatily_pic'];
//        $datas['title']=$data['title'];
            $datas['join_type']=$data['quality_join_type'];
            $datas['join_type_sub']=$data['quality_join_type_sub'];
            $datas['video_url']=$data['quality_video_url'];
            $datas['video_desc']=$data['quality_desc'];
            $datas['duration']=$data['duration'];
            $datas['examine']=3;
            $datas['type']=1;
            $datas['quality']=1;
            $datas['join_type_sub_name']=$data['join_type_sub_name'];
            $datas['is_dub']=3;
            $datas['add_time']=date("Y-m-d H:i:s",time());
            $this->db->trans_begin();// 开启事务
            //添加
            $this->db->insert($this->table3,$datas);
            $user_material_id=$this->db->insert_id();
            //受影响的行数
            $arr=$this->db->affected_rows();

            $data['user_material_id']=$user_material_id;
            $data['is_join']=2;
            $data['online']=2;
            //添加
            $this->db->update($this->table,$data,['id'=>$data['id']]);

            if($this->db->trans_status()===false || $arr==0){
                $this->db->trans_rollback();//回滚
                return false;
            }else{
                $this->db->trans_commit();//提交事物

                return $user_material_id;
            }
        }


    }

    /**
     * 移出话题
     */
    public function move_material($data){
        $this->db->trans_begin();// 开启事务
        //先修改
        $datas['is_join']=3;
        $datas['user_material_id']="";
        $this->db->update($this->table,$datas,["id"=>$data['id']]);
        //受影响的行数
        $arr=$this->db->affected_rows();
        //删除
        $this->db->delete($this->table3,['id'=>$data['user_material_id']]);

        if($this->db->trans_status()===false || $arr==0){
            $this->db->trans_rollback();//回滚
            return false;
        }else{
            $this->db->trans_commit();//提交事物
            return true;
        }
    }

    /**
     * 删除精品视频
     */
    public function drop_quality($video_uuid){
        //先去删除user_material表的，再去删除quality_video表
        $ones=$this->one($this->table,['id'=>$video_uuid]);
        //查询要删除的数据入库
        $res1=$this->one($this->table3,['id'=>$ones['user_material_id']]);
        $ones['user_material']=$res1;
        if(empty($ones)){
            return false;
        }
        $this->db->trans_begin();//开启事物
        $this->db->delete($this->table,['id'=>$ones['id']]);
        //受影响的行数
        $arr=$this->db->affected_rows();
        $this->db->delete($this->table3,['id'=>$ones['user_material_id']]);
        if($this->db->trans_status()===false || $arr==0){
            $this->db->trans_rollback();//回滚
            return false;
        }else{
            $this->db->trans_commit();//提交事物
            return $ones;
        }
    }

    /**
     * 修改加入话题的精品视频
     */
    public function edit_material($data,$res){
        $this->db->trans_begin();// 开启事务
        $datas['join_type']=$data['quality_join_type'];
        $datas['join_type_sub']=$data['quality_join_type_sub'];
        $datas['video_url']=$data['quality_video_url'];
        $datas['pic']=$data['quatily_pic'];
        $datas['share_pic']=$data['quality_share_pic'];
        $datas['video_desc']=$data['quality_desc'];
        $datas['duration']=$data['duration'];
        $datas['width2']=$data['width1'];
        $datas['height2']=$data['height1'];
        $datas['first_pic3']=$data['first_pic2'];
        $this->db->update($this->table3,$datas,["id"=>$res['user_material_id']]);
        //受影响的行数
        $arr=$this->db->affected_rows();
        $this->db->update($this->table,$data,["id"=>$res['id']]);
        if($this->db->trans_status()===false || $arr==0){
            $this->db->trans_rollback();//回滚
            return false;
        }else{
            $this->db->trans_commit();//提交事物
            return true;
        }
    }

    /**
     * 把配音精品加入到user_material表和精品视频表，加入就是精品
     */
    public function add_quality_dub($data,$video){
        $datas['uuid']=$data['uuid'];
        $datas['video_uuid']=$data['quality_uuid'];
        $datas['nick_name']=$data['nick_name'];
        $datas['nick_pic']=$data['nick_pic'];
        $datas['pic']=$data['quatily_pic'];
        $datas['share_pic']=$data['quality_share_pic'];
        $datas['join_type']=$data['quality_join_type'];
        $datas['join_type_sub']=$data['quality_join_type_sub'];
        $datas['duration']=$data['duration'];
        $datas['video_desc']=$data['quality_desc'];
        $datas['no_music_video_url']=$video['transcode_video_no_music_url'];
        $datas['music']=$video['cut_music_url'];
        $datas['video_url']=$video['transcode_video_url'];
        $datas['examine']=3;//审核通过
        $datas['quality']=2;//加入精选列表
        $datas['is_save']=1;//已经保存
        $datas['mix_status']=1;//合成状态成功
        $datas['is_user_zj']=2;//合成状态成功
        $datas['add_time']=date("Y-m-d H:i:s",time());
        $datas['width2']=$video['width'];
        $datas['height2']=$video['height'];
        $datas['first_pic3']=$video['first_pic2'];
        $this->db->trans_begin();//开启事物
        $user_material_id=$this->add($this->table3,$datas);
        //受影响的行数
        $arr=$this->db->affected_rows();
        $data['user_material_id']=$user_material_id;
        $this->add($this->table,$data);
        if($this->db->trans_status()===false || $arr==0){
            $this->db->trans_rollback();//回滚
            return false;
        }else{
            $this->db->trans_commit();//提交事物
            return true;
        }
    }

    /**
     * 把配音精品加入到user_material表和精品视频表
     */
    public function add_dub($data,$video){
        $datas['uuid']=$data['uuid'];
        $datas['video_uuid']=$data['quality_uuid'];
        $datas['nick_name']=$data['nick_name'];
        $datas['nick_pic']=$data['nick_pic'];
        $datas['pic']=$data['quatily_pic'];
        $datas['share_pic']=$data['quality_share_pic'];
        $datas['join_type']=$data['quality_join_type'];
        $datas['join_type_sub']=$data['quality_join_type_sub'];
        $datas['duration']=$data['duration'];
        $datas['video_desc']=$data['quality_desc'];
        $datas['no_music_video_url']=$video['transcode_video_no_music_url'];
        $datas['music']=$video['cut_music_url'];
        $datas['video_url']=$video['transcode_video_url'];
        $datas['examine']=3;//审核通过
        $datas['quality']=1;//加入普通列表
        $datas['is_save']=1;//已经保存
        $datas['mix_status']=1;//合成状态成功
        $datas['is_user_zj']=2;//合成状态成功
        $datas['add_time']=date("Y-m-d H:i:s",time());
        $datas['width2']=$video['width'];
        $datas['height2']=$video['height'];
        $datas['first_pic3']=$video['first_pic2'];
        $this->db->trans_begin();//开启事物
        $user_material_id=$this->add($this->table3,$datas);
        //受影响的行数
        $arr=$this->db->affected_rows();
        $data['user_material_id']=$user_material_id;
        $this->add($this->table,$data);
        if($this->db->trans_status()===false || $arr==0){
            $this->db->trans_rollback();//回滚
            return false;
        }else{
            $this->db->trans_commit();//提交事物
            return true;
        }
    }

    /**
     * 查询分类
     */
    public function types(){
        $this->db->select('*');
        $this->db->from($this->table1);
        $this->db->where("is_other !=",999999999);
        //这是不加入已挑选视频
        $this->db->where("online",2);
        $res = $this->db->get();
        $datas= $res->result_array();
//        echo $this->db->last_query();
        return $datas;
    }

    /**
     * 查询话题
     */
    public function type_sub($where=[],$type=1){
        $this->db->select('*');
        $this->db->from($this->table2);
        //这是不加入已挑选视频
        if($type==1){
            $this->db->where("online",2);
            $this->db->where($where);
        }
        $res = $this->db->get();
        $datas= $res->result_array();
//        echo $this->db->last_query();
        return $datas;
    }

    /**
     * 查询所有用户
     */
    public function all_users(){
        $this->db->select('uuid,nick_name');
        $this->db->from($this->table4);
        //这是不加入已挑选视频
        $this->db->where("user_type",2);
        $res = $this->db->get();
        $datas= $res->result_array();
//        echo $this->db->last_query();
        return $datas;
    }

    /**
     * 查询关联的原始素材
     */
    public function material($where=[],$type=1,$select=""){
        if(empty($select)){
            $this->db->select('*');
        }else{
            $this->db->select($select);
        }
        $this->db->from($this->table5);
        //这是不加入已挑选视频
        if($type==1){
            $this->db->where("video_type",3);
            $this->db->where("status",2);
            $this->db->where($where);
        }
        $res = $this->db->get();
        $datas= $res->result_array();
//        echo $this->db->last_query();
        return $datas;
    }

    /**
     * 查询关联的原始素材单个详情
     */
    public function material_detail($where=[],$type=1,$select=""){
        if(empty($select)){
            $this->db->select('*');
        }else{
            $this->db->select($select);
        }
        $this->db->from($this->table5);
        //这是不加入已挑选视频
        if($type==1){
            $this->db->where("video_type",3);
            $this->db->where("status",2);
            $this->db->where($where);
        }
        $res = $this->db->get();
        $datas= $res->result_array();
//        echo $this->db->last_query();
        return $datas;
    }

    /**
     * 修改精品视频
     */
    public function edit_dub_save($data,$video,$user_material_id,$id1){
        $data1['uuid']=$data['uuid'];
        $data1['nick_name']=$data['nick_name'];
        $data1['nick_pic']=$data['nick_pic'];
        $data1['join_type']=$data['quality_join_type'];
        $data1['join_type_sub']=$data['quality_join_type_sub'];
        $data1['video_uuid']=$data['quality_uuid'];
        $data1['video_url']=$data['quality_video_url'];
        $data1['no_music_video_url']=$video['transcode_video_no_music_url'];
        $data1['music']=$video['cut_music_url'];
        $data1['pic']=$data['quatily_pic'];
        $data1['share_pic']=$data['quality_share_pic'];
        $data1['video_desc']=$data['quality_desc'];
        $data1['duration']=$video['video_time'];
        $data1['width2']=$video['width'];
        $data1['height2']=$video['height'];
        $data1['first_pic3']=$video['first_pic2'];
        $this->db->trans_begin();//开启事物
        $this->update($this->table3,$data1,['id'=>$user_material_id]);
        //受影响的行数
        $arr=$this->db->affected_rows();
        $this->update($this->table,$data,['id'=>$id1]);
        if($this->db->trans_status()===false || $arr==0){
            $this->db->trans_rollback();//回滚
            return false;
        }else{
            $this->db->trans_commit();//提交事物
            return true;
        }
    }

}